package contratosonline.ejb.controller;


import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import contratosonline.entity.Contrato;
import contratosonline.entity.FiscalContrato;
import contratosonline.entity.Setor;

import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author admin
 */
@Stateless
public class FiscalJPAController implements FiscalJPAControllerLocal{
	@PersistenceContext
	private EntityManager em;

    public EntityManager getEntityManager() {
        return em;
    }
	
	public FiscalJPAController(){
	
	}

    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalControllerLocal#persiste(contratosonline.entity.FiscalContrato)
	 */
    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalJPAControllerLocal#persiste(contratosonline.entity.FiscalContrato)
	 */
    public void persiste(FiscalContrato fiscalcontrato) {
        if (fiscalcontrato.getContratoCollection() == null) {
            fiscalcontrato.setContratoCollection(new ArrayList<Contrato>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            Setor setor = fiscalcontrato.getSetor();
            if (setor != null) {
                setor = em.getReference(setor.getClass(), setor.getIdSetor());
                fiscalcontrato.setSetor(setor);
            }
            Collection<Contrato> attachedContratoCollection = new ArrayList<Contrato>();
            for (Contrato contratoCollectionContratoToAttach : fiscalcontrato.getContratoCollection()) {
                contratoCollectionContratoToAttach = em.getReference(contratoCollectionContratoToAttach.getClass(), contratoCollectionContratoToAttach.getIdcontrato());
                attachedContratoCollection.add(contratoCollectionContratoToAttach);
            }
            fiscalcontrato.setContratoCollection(attachedContratoCollection);
            em.persist(fiscalcontrato);
            if (setor != null) {
                setor.getFiscalcontratoCollection().add(fiscalcontrato);
                setor = em.merge(setor);
            }
            for (Contrato contratoCollectionContrato : fiscalcontrato.getContratoCollection()) {
                FiscalContrato oldFiscalOfContratoCollectionContrato = contratoCollectionContrato.getFiscal();
                contratoCollectionContrato.setFiscal(fiscalcontrato);
                contratoCollectionContrato = em.merge(contratoCollectionContrato);
                if (oldFiscalOfContratoCollectionContrato != null) {
                    oldFiscalOfContratoCollectionContrato.getContratoCollection().remove(contratoCollectionContrato);
                    oldFiscalOfContratoCollectionContrato = em.merge(oldFiscalOfContratoCollectionContrato);
                }
            }
        } finally {
            if (em != null) {
              // em.close();
            }
        }
    }

    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalControllerLocal#altera(contratosonline.entity.FiscalContrato)
	 */
    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalJPAControllerLocal#altera(contratosonline.entity.FiscalContrato)
	 */
    public void altera(FiscalContrato fiscalcontrato) throws Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            FiscalContrato persistentFiscalcontrato = em.find(FiscalContrato.class, fiscalcontrato.getIdFiscalContrato());
            Setor setorOld = persistentFiscalcontrato.getSetor();
            Setor setorNew = fiscalcontrato.getSetor();
            Collection<Contrato> contratoCollectionOld = persistentFiscalcontrato.getContratoCollection();
            Collection<Contrato> contratoCollectionNew = fiscalcontrato.getContratoCollection();
            if (setorNew != null) {
                setorNew = em.getReference(setorNew.getClass(), setorNew.getIdSetor());
                fiscalcontrato.setSetor(setorNew);
            }
            Collection<Contrato> attachedContratoCollectionNew = new ArrayList<Contrato>();
            for (Contrato contratoCollectionNewContratoToAttach : contratoCollectionNew) {
                contratoCollectionNewContratoToAttach = em.getReference(contratoCollectionNewContratoToAttach.getClass(), contratoCollectionNewContratoToAttach.getIdcontrato());
                attachedContratoCollectionNew.add(contratoCollectionNewContratoToAttach);
            }
            contratoCollectionNew = attachedContratoCollectionNew;
            fiscalcontrato.setContratoCollection(contratoCollectionNew);
            fiscalcontrato = em.merge(fiscalcontrato);
            if (setorOld != null && !setorOld.equals(setorNew)) {
                setorOld.getFiscalcontratoCollection().remove(fiscalcontrato);
                setorOld = em.merge(setorOld);
            }
            if (setorNew != null && !setorNew.equals(setorOld)) {
                setorNew.getFiscalcontratoCollection().add(fiscalcontrato);
                setorNew = em.merge(setorNew);
            }
            for (Contrato contratoCollectionOldContrato : contratoCollectionOld) {
                if (!contratoCollectionNew.contains(contratoCollectionOldContrato)) {
                    contratoCollectionOldContrato.setFiscal(null);
                    contratoCollectionOldContrato = em.merge(contratoCollectionOldContrato);
                }
            }
            for (Contrato contratoCollectionNewContrato : contratoCollectionNew) {
                if (!contratoCollectionOld.contains(contratoCollectionNewContrato)) {
                    FiscalContrato oldFiscalOfContratoCollectionNewContrato = contratoCollectionNewContrato.getFiscal();
                    contratoCollectionNewContrato.setFiscal(fiscalcontrato);
                    contratoCollectionNewContrato = em.merge(contratoCollectionNewContrato);
                    if (oldFiscalOfContratoCollectionNewContrato != null && !oldFiscalOfContratoCollectionNewContrato.equals(fiscalcontrato)) {
                        oldFiscalOfContratoCollectionNewContrato.getContratoCollection().remove(contratoCollectionNewContrato);
                        oldFiscalOfContratoCollectionNewContrato = em.merge(oldFiscalOfContratoCollectionNewContrato);
                    }
                }
            }
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = fiscalcontrato.getIdFiscalContrato();
                if (findFiscalcontrato(id) == null) {
                    throw new Exception("The fiscalcontrato with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                //em.close();
            }
        }
    }

    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalControllerLocal#exclui(java.lang.Integer)
	 */
    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalJPAControllerLocal#exclui(java.lang.Integer)
	 */
    public void exclui(Integer id) throws Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            FiscalContrato fiscalcontrato;
            try {
                fiscalcontrato = em.getReference(FiscalContrato.class, id);
                fiscalcontrato.getIdFiscalContrato();
            } catch (EntityNotFoundException enfe) {
                throw new Exception("The fiscalcontrato with id " + id + " no longer exists.", enfe);
            }
            Setor setor = fiscalcontrato.getSetor();
            if (setor != null) {
                setor.getFiscalcontratoCollection().remove(fiscalcontrato);
                setor = em.merge(setor);
            }
            Collection<Contrato> contratoCollection = fiscalcontrato.getContratoCollection();
            for (Contrato contratoCollectionContrato : contratoCollection) {
                contratoCollectionContrato.setFiscal(null);
                contratoCollectionContrato = em.merge(contratoCollectionContrato);
            }
            em.remove(fiscalcontrato);
        } finally {
            if (em != null) {
                //em.close();
            }
        }
    }

    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalControllerLocal#findFiscalcontratoEntities()
	 */
    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalJPAControllerLocal#findFiscalcontratoEntities()
	 */
    public List<FiscalContrato> findFiscalcontratoEntities() {
        return findFiscalcontratoEntities(true, -1, -1);
    }

    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalControllerLocal#findFiscalcontratoEntities(int, int)
	 */
    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalJPAControllerLocal#findFiscalcontratoEntities(int, int)
	 */
    public List<FiscalContrato> findFiscalcontratoEntities(int maxResults, int firstResult) {
        return findFiscalcontratoEntities(false, maxResults, firstResult);
    }

    @SuppressWarnings("unchecked")
	private List<FiscalContrato> findFiscalcontratoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Fiscalcontrato as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            //em.close();
        }
    }

    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalControllerLocal#findFiscalcontrato(java.lang.Integer)
	 */
    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalJPAControllerLocal#findFiscalcontrato(java.lang.Integer)
	 */
    public FiscalContrato findFiscalcontrato(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FiscalContrato.class, id);
        } finally {
            //em.close();
        }
    }

    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalControllerLocal#getFiscalcontratoCount()
	 */
    /* (non-Javadoc)
	 * @see contratosonline.ejb.controller.FiscalJPAControllerLocal#getFiscalcontratoCount()
	 */
    public int getFiscalcontratoCount() {
        EntityManager em = getEntityManager();
        try {
            return ((Long) em.createQuery("select count(o) from Fiscalcontrato as o").getSingleResult()).intValue();
        } finally {
           // em.close();
        }
    }

}

